package com.jscs;
import com.jscs.settings.Settings;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationListener;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.components.ProjectComponent;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.wix.utils.FileUtils;
import com.wix.utils.FileUtils.ValidationStatus;
import org.jetbrains.annotations.NotNull;
import javax.swing.event.HyperlinkEvent;
public class JscsProjectComponent implements ProjectComponent {
public static final String FIX_CONFIG_HREF = "\n<a href=\"#\">Fix Configuration</a>";
protected Project project;
public Settings settings;
protected boolean settingValidStatus;
protected String settingValidVersion;
protected String settingVersionLastShowNotification;
private static final Logger LOG = Logger.getInstance(JscsBundle.LOG_ID);
public String jscsRcFile;
public String preset;
public String jscsExecutable;
public String nodeInterpreter;
public boolean treatAsWarnings;
public boolean pluginEnabled;
public static final String PLUGIN_NAME = "JSCS";
public JscsProjectComponent(Project project) {
this.project = project;
settings = Settings.getInstance(project);
}
@Override
public void projectOpened() {
if (isEnabled()) {
isSettingsValid();
}
}
@Override
public void projectClosed() {
}
@Override
public void initComponent() {
if (isEnabled()) {
isSettingsValid();
}
}
@Override
public void disposeComponent() {
}
@NotNull
@Override
public String getComponentName() {
return "JscsProjectComponent";
}
public boolean isEnabled() {
return Settings.getInstance(project).pluginEnabled;
}
public boolean isSettingsValid() {
if (!settings.getVersion().equals(settingValidVersion)) {
validateSettings();
settingValidVersion = settings.getVersion();
}
return settingValidStatus;
}
public boolean shouldLint() {
return isEnabled() && isSettingsValid();
}
public boolean validateSettings() {
// do not validate if disabled
if (!settings.pluginEnabled) {
return true;
}
boolean status = validateField("Node Interpreter", settings.nodeInterpreter, true, false, true);
if (!status) {
return false;
}
status = validateField("JSCS bin", settings.jscsExecutable, false, false, true);
if (!status) {
return false;
}
// if (StringUtil.isNotEmpty(settings.jscsExecutable)) {
// File file = new File(project.getBasePath(), settings.jscsExecutable);
// if (!file.exists()) {
// showErrorConfigNotification(JscsBundle.message("jscs.rules.dir.does.not.exist", file.toString()));
// LOG.debug("Rules directory not found");
// settingValidStatus = false;
// return false;
// }
// }
jscsExecutable = settings.jscsExecutable;
jscsRcFile = settings.jscsrcFile;
preset = settings.preset;
nodeInterpreter = settings.nodeInterpreter;
treatAsWarnings = settings.treatAllIssuesAsWarnings;
pluginEnabled = settings.pluginEnabled;
settingValidStatus = true;
return true;
}
private boolean validateField(String fieldName, String value, boolean shouldBeAbsolute, boolean allowEmpty, boolean isFile) {
ValidationStatus r = FileUtils.validateProjectPath(shouldBeAbsolute ? null : project, value, allowEmpty, isFile);
if (isFile) {
if (r == ValidationStatus.NOT_A_FILE) {
String msg = JscsBundle.message("jscs.file.is.not.a.file", fieldName, value);
validationFailed(msg);
return false;
}
} else {
if (r == ValidationStatus.NOT_A_DIRECTORY) {
String msg = JscsBundle.message("jscs.directory.is.not.a.dir", fieldName, value);
validationFailed(msg);
return false;
}
}
if (r == ValidationStatus.DOES_NOT_EXIST) {
String msg = JscsBundle.message("jscs.file.does.not.exist", fieldName, value);
validationFailed(msg);
return false;
}
return true;
}
private void validationFailed(String msg) {
NotificationListener notificationListener = new NotificationListener() {
@Override
public void hyperlinkUpdate(@NotNull Notification notification, @NotNull HyperlinkEvent event) {
JscsInspection.showSettings(project);
}
};
String errorMessage = msg + FIX_CONFIG_HREF;
showInfoNotification(errorMessage, NotificationType.WARNING, notificationListener);
LOG.debug(msg);
settingValidStatus = false;
}
protected void showErrorConfigNotification(String content) {
if (!settings.getVersion().equals(settingVersionLastShowNotification)) {
settingVersionLastShowNotification = settings.getVersion();
showInfoNotification(content, NotificationType.WARNING);
}
}
public void showWarn(String content) {
showInfoNotification(content, NotificationType.WARNING);
}
public void showInfoNotification(String content, NotificationType type) {
Notification errorNotification = new Notification(PLUGIN_NAME, PLUGIN_NAME, content, type);
Notifications.Bus.notify(errorNotification, this.project);
}
public void showInfoNotification(String content, NotificationType type, NotificationListener notificationListener) {
Notification errorNotification = new Notification(PLUGIN_NAME, PLUGIN_NAME, content, type, notificationListener);
Notifications.Bus.notify(errorNotification, this.project);
}
public static void showNotification(String content, NotificationType type) {
Notification errorNotification = new Notification(PLUGIN_NAME, PLUGIN_NAME, content, type);
Notifications.Bus.notify(errorNotification);
}
public static void showError(String content) {
showNotification(content, NotificationType.ERROR);
}
}